package com.example.wechatlistview.events.controllers;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.wechatlistview.R;
import com.example.wechatlistview.controls.ListSection;
import com.example.wechatlistview.events.strategys.ClickStrategy;
import com.example.wechatlistview.resmanager.itemType;
import com.example.wechatlistview.resmanager.DataSource;
import com.example.wechatlistview.tools.CustomTools;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

/**
 * 列表项点击事件总控制器
 *
 * @author CVLocke 陈炜龙
 */
public class ClickController {


    /**
     * Holder事件控制器内部类
     */
    public static class HolderBuilder implements ListSection.OnRecyclerViewItemClickListener {
        private final Context mContext;
        private final DataSource mDataSource;

        /**
         * Holder事件总控制器构造函数
         *
         * @param context 上下文对象
         */
        public HolderBuilder(Context context) {
            mContext = context;
            mDataSource = new DataSource(context);
        }

        /**
         * 根据不同类型实现点击事件
         *
         * @param v        控件
         * @param position 位置
         */
        @Override
        public void onItemClick(View v, int position) {
            String tag = v.getTag().toString();
            tagManager(CustomTools.getActivityFromView(v), tag);
        }


    }

    /**
     * Button按钮事件控制器内部类
     */
    public static class BtnBuilder implements View.OnClickListener {

        private final Context mContext;
        private final DataSource mDataSource;
        private final ClickStrategy.BtnStratgy btnType;

        /**
         * 控制器构造函数
         *
         * @param context 上下文对象
         * @param type    按钮类型
         */
        public BtnBuilder(Context context, @NonNull ClickStrategy.BtnStratgy type) {
            mContext = context;
            mDataSource = new DataSource(context);
            btnType = type;
        }

        //region Toast Setting

        /**
         * 设置Toast内容
         *
         * @param text 文本
         * @return BtnBuilder
         */
        public BtnBuilder setToast(@Nullable CharSequence text, @Nullable Integer time) {
            if (text != null) {
                Param.pToast.text = text;
            }
            if (time != null) {
                Param.pToast.time = time;
            }
            return this;
        }
        //endregion


        /**
         * 根据不同类型实现点击事件
         *
         * @param v 控件
         */
        @Override
        public void onClick(View v) {
            btnManager(v, btnType);
        }


        /**
         * 参数类集
         */
        public static class Param {
            public static class pToast {
                public static CharSequence text = "null";
                public static int time = Toast.LENGTH_SHORT;
            }
        }

    }


    /**
     * 按钮类型->策略中继器
     *
     * @param view 当前控件
     * @param type 传入预设按钮枚举，访问策略组处理对应方法
     */
    private static void btnManager(View view, ClickStrategy.BtnStratgy type) {
        try {
            ClickStrategy.BtnStratgy strategy = type;
            strategy.run(view, null);
        } catch (Exception e) {
            Toast.makeText(view.getContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 标签->策略中继器
     *
     * @param context 上下文对象
     * @param tag     传入标签访问方法策略以链接不同页面
     */
    private static void tagManager(Context context, String tag) {
        try {
            ClickStrategy.TagStrategy strategy = ClickStrategy.TagStrategy.valueOf(tag);
            strategy.run(context, tag);
        } catch (Exception e) {
            Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
        }
    }

}
